package com.ming.quartz.case1.jobDetail;

import com.ming.quartz.case1.job.OneJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OneScheduler {

    @Bean
    public JobDetail oneJobDetail() {
        // 链式编程,可以携带多个参数,在Job类中声明属性 + setter方法
//        withIdentity 方法可以传入两个参数 withIdentity(String name,String group) 来定义 TriggerKey，
//        也可以不设置，像上文示例中会自动生成一个独一无二的 TriggerKey 用来区分不同的 Trigger。
        return JobBuilder.newJob(OneJob.class).withIdentity("oneJob")
                .usingJobData("one","one").storeDurably().build();
    }

    @Bean
    public Trigger oneJobTrigger(){
        // 每隔两秒执行一次
        SimpleScheduleBuilder scheduleBuilder =
                SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(2)
                        .repeatForever();
        return TriggerBuilder
                .newTrigger()
                .forJob(oneJobDetail())
                .withIdentity("oneJob")
                .withSchedule(scheduleBuilder)
                .build();
    }

}